<template>
	<view>
		<u-popup :show="show" borderRadius="20" :closeable="false">
			<view class="main">
				<view class="u-flex u-m-t-40">
					<u-image width="40" height="40" :src="logo"></u-image>
					<text class="u-m-l-20">{{ company }} 申请</text>
				</view>
				<!-- #ifdef MP-WEIXIN -->
				<view class="u-m-t-40">申请获取你微信绑定的手机号:</view>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<view class="u-m-t-40">绑定手机号:</view>
				<!-- #endif -->
				<u-line class="u-line" margin="40rpx 0" color="#e5e5e5" :hair-line="false"></u-line>
				<view class="u-flex u-flex-center">
					<view class="btnno u-m-r-30" @click="chuan">拒绝</view>
					<!-- #ifdef MP-WEIXIN -->
					<u-button :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getphonenumber" :custom-style="loginStyle">允许</u-button>
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<view class="btnyes" @click="btnyes">允许</view>
					<!-- #endif -->
				</view>
			</view>
		</u-popup>
	</view>
</template>
<script>
export default {
	name: 'obtain',
	props: {
		show: Boolean
	},
	data() {
		return {
			loginStyle: {
				width: '180rpx',
				height: '60rpx',
				fontSize: '28rpx',
				backgroundColor: '#07c160',
				borderRadius: '6rpx',
				border: 0,
				color: '#fff',
				margin: 0
			}
		};
	},
	methods: {
		chuan() {
			this.$emit('showchange');
		},
		btnyes() {
			this.nextNav('/pages/login/mobile');
		},
		// 授权手机号
		getphonenumber(e) {
			const userId = uni.getStorageSync('userId');
			let that = this;
			uni.login({
				success: async loginRes => {
					let params = {
						code: loginRes.code,
						encryptedData: encodeURIComponent(e.detail.encryptedData),
						iv: encodeURIComponent(e.detail.iv)
					};
					let res = await this.require('encryptedphone', params);
					console.log(res);
					if (res) {
						that.$u.toast('授权手机号成功！');
						try {
							uni.setStorageSync('userInfo', JSON.stringify(res));
							uni.setStorageSync('mobile', res.mobile);
							uni.setStorageSync('rong_token', res.rong_token);
							if (uni.getStorageSync('rong_token')) {
								this.$ry.connect({token: uni.getStorageSync('rong_token')}).then(async user => {
									console.log('链接成功, 链接用户 id 为: ', user.id);
									uni.setStorageSync('senderUserId', user.id);
									that.xinxiao();
									await that.require('postrongyunid',{senderUserId: user.id})
									uni.setStorageSync('userInfo', JSON.stringify({
										...res,
										senderUserId: user.id
									}));
								}).catch(error => {
									console.log('链接失败: ', error.code, error.msg);
								});
							}
							that.$emit('showchange');
						} catch (e) {
							console.log(e);
						}
					}
				}
			});
		},
	}
};
</script>
<style lang="scss">
.main {
	background-color: #ededed;
	padding: 14rpx 30rpx 60rpx;
}

.btnno {
	width: 180rpx;
	height: 60rpx;
	line-height: 60rpx;
	text-align: center;
	border-radius: 6rpx;
	box-shadow: 0 3px 10px 2px rgba(166, 166, 166, 0.1);
	color: #07c160;
	background-color: #fff;
}

.btnyes {
	width: 180rpx;
	height: 60rpx;
	line-height: 60rpx;
	text-align: center;
	border-radius: 6rpx;
	box-shadow: 0 3px 10px 2px rgba(166, 166, 166, 0.1);
	color: #fff;
	background-color: #07c160;
}
</style>
